home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / dev / mui / mui37_de.lha / Amiga-E / Examples / Class2.e < prev    next >
Text File  |  1996-08-25  |  10KB  |  340 lines

  1. /*
  2. ** Demosource on how to use customclasses in E.
  3. ** Based on the C example 'Class2.c' by Stafan Stuntz.
  4. ** Translated TO E by Sven Steiniger
  5. **
  6. ** Sorry FOR some uppercase words in the comments. This IS because OF
  7. ** my AutoCase-dictionary
  8. */
  9.  
  10. OPT PREPROCESS
  11.  
  12. MODULE 'muimaster','libraries/mui','libraries/muip',
  13.        'intuition/classes','intuition/classusr','intuition/screens','intuition/intuition',
  14.        'utility','utility/tagitem',
  15.        'amigalib/boopsi',
  16.        'mui/muicustomclass'
  17.  
  18. /***************************************************************************/
  19. /* Here is the beginning of our simple new class...                        */
  20. /***************************************************************************/
  21.  
  22. /*
  23. ** This class is the same as within Class1.c except that it features
  24. ** a pen attribute.
  25. */
  26.  
  27. OBJECT mydata
  28.     penspec:mui_penspec
  29.     pen:LONG
  30.     penchange
  31. ENDOBJECT
  32.  
  33. CONST MYATTR_PEN=$8022   /* tag value for the new attribute.            */
  34.  
  35.  
  36. PROC mNew(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO opset)
  37. DEF data:PTR TO mydata,
  38.     tags:PTR TO tagitem,
  39.     tag:PTR TO tagitem
  40.  
  41.   IF (obj:=doSuperMethodA(cl,obj,msg))=NIL THEN RETURN 0
  42.  
  43.   data:=INST_DATA(cl,obj)
  44.  
  45.   /* parse initial taglist */
  46.   tags:=msg.attrlist
  47.   WHILE tag:=NextTagItem({tags})
  48.     IF tag.tag=MYATTR_PEN THEN
  49.       IF tag.data THEN CopyMem(tag.data,data.penspec,SIZEOF mui_penspec)
  50.   ENDWHILE
  51.  
  52. ENDPROC obj
  53.  
  54.  
  55. /* OM_NEW didnt allocates something, just DO nothing here... */
  56.  
  57. PROC mDispose(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO msg) IS
  58.   doSuperMethodA(cl,obj,msg)
  59.  
  60.  
  61. PROC mSet(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO opset)
  62. DEF data:PTR TO mydata,
  63.     tags:PTR TO tagitem,
  64.     tag:PTR TO tagitem
  65.  
  66.   data:=INST_DATA(cl,obj)
  67.   tags:=msg.attrlist
  68.   WHILE tag:=NextTagItem({tags})
  69.     IF tag.tag=MYATTR_PEN THEN
  70.       IF tag.data
  71.         CopyMem(tag.data,data.penspec,SIZEOF mui_penspec)
  72.         data.penchange:=TRUE
  73.         Mui_Redraw(obj,MADF_DRAWOBJECT)  /* redraw ourselves completely */
  74.       ENDIF
  75.   ENDWHILE
  76.  
  77. ENDPROC doSuperMethodA(cl,obj,msg)
  78.  
  79.  
  80. /*
  81. ** OM_GET method, see if someone wants to read the color.
  82. */
  83.  
  84. PROC mGet(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO opget)
  85. DEF data:PTR TO mydata,storage
  86.  
  87.   IF msg.attrid=MYATTR_PEN
  88.     data:=INST_DATA(cl,obj)
  89.     storage:=msg.storage
  90.     ^storage:=data.penspec
  91.     RETURN MUI_TRUE
  92.   ENDIF
  93.  
  94. ENDPROC doSuperMethodA(cl,obj,msg)
  95.  
  96.  
  97. PROC mSetup(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO msg)
  98. DEF data:PTR TO mydata
  99.  
  100.   IF doSuperMethodA(cl,obj,msg)=FALSE THEN RETURN FALSE
  101.   data:=INST_DATA(cl,obj)
  102.   data.pen:=Mui_ObtainPen(muiRenderInfo(obj),data.penspec,0)
  103.  
  104. ENDPROC MUI_TRUE
  105.  
  106.  
  107. PROC mCleanup(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO msg)
  108. DEF data:PTR TO mydata
  109.  
  110.   data:=INST_DATA(cl,obj)
  111.   Mui_ReleasePen(muiRenderInfo(obj),data.pen)
  112.  
  113. ENDPROC doSuperMethodA(cl,obj,msg)
  114.  
  115.  
  116. /*
  117. ** AskMinMax method will be called before the window is opened
  118. ** and before layout takes place. We need to tell MUI the
  119. ** minimum, maximum and default size of our object.
  120. */
  121.  
  122. PROC mAskMinMax(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_askminmax)
  123.  
  124.   /*
  125.   ** let our superclass first fill in what it thinks about sizes.
  126.   ** this will e.g. add the size OF frame and inner spacing.
  127.   */
  128.  
  129.   doSuperMethodA(cl,obj,msg)
  130.  
  131.   /*
  132.   ** now add the values specific TO our object. note that we
  133.   ** indeed need TO *add* these values, not just set them!
  134.   */
  135.  
  136.   msg.minmaxinfo.minwidth := msg.minmaxinfo.minwidth + 100
  137.   msg.minmaxinfo.defwidth := msg.minmaxinfo.defwidth + 120
  138.   msg.minmaxinfo.maxwidth := msg.minmaxinfo.maxwidth + 500
  139.  
  140.   msg.minmaxinfo.minheight := msg.minmaxinfo.minheight + 40
  141.   msg.minmaxinfo.defheight := msg.minmaxinfo.defheight + 90
  142.   msg.minmaxinfo.maxheight := msg.minmaxinfo.maxheight + 300
  143.  
  144. ENDPROC 0
  145.  
  146.  
  147. /*
  148. ** Draw method is called whenever MUI feels we should render
  149. ** our object. This usually happens after layout is finished
  150. ** or when we need to refresh in a simplerefresh window.
  151. ** Note: You may only render within the rectangle
  152. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  153. */
  154.  
  155. PROC mDraw(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO muip_draw)
  156. DEF data:PTR TO mydata,
  157.     i
  158.  
  159.   data:=INST_DATA(cl,obj)
  160.  
  161.   /*
  162.   ** let our superclass draw itself first, area class would
  163.   ** e.g. draw the frame and clear the whole region. What
  164.   ** it does exactly depends on msg.flags.
  165.   */
  166.  
  167.   doSuperMethodA(cl,obj,msg)
  168.  
  169.   /*
  170.   ** IF MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  171.   ** MUI just wanted TO update the frame OR something like that.
  172.   */
  173.  
  174.   IF (msg.flags AND MADF_DRAWOBJECT)=0 THEN RETURN 0
  175.  
  176.   /*
  177.   ** test IF someone changed our pen
  178.   */
  179.  
  180.   IF data.penchange
  181.     data.penchange:=FALSE
  182.     Mui_ReleasePen(muiRenderInfo(obj),data.pen)
  183.     data.pen:=Mui_ObtainPen(muiRenderInfo(obj),data.penspec,0)
  184.   ENDIF
  185.  
  186.  
  187.   /*
  188.   ** ok, everything ready TO render...
  189.   ** Note that we *must* use the MUIPEN() macro before actually
  190.   ** using pens from MUI_ObtainPen() in rendering calls.
  191.   */
  192.  
  193.   SetAPen(_rp(obj),MUIPEN(data.pen))
  194.  
  195.   FOR i:=_mleft(obj) TO _mright(obj) STEP 5
  196.     Move(_rp(obj),_mleft(obj),_mbottom(obj))
  197.     Draw(_rp(obj),i,_mtop(obj))
  198.     Move(_rp(obj),_mright(obj),_mbottom(obj))
  199.     Draw(_rp(obj),i,_mtop(obj))
  200.   ENDFOR
  201.  
  202. ENDPROC 0
  203.  
  204.  
  205. /*
  206. ** Here comes the dispatcher FOR our custom class.
  207. ** Unknown/unused methods are passed to the superclass immediately.
  208. */
  209.  
  210. PROC myDispatcher(cl:PTR TO iclass,obj:PTR TO object,msg:PTR TO msg)
  211. DEF methodID
  212.  
  213.   methodID:=msg.methodid
  214.   SELECT methodID
  215.     CASE OM_NEW          ;  RETURN mNew      (cl,obj,msg)
  216.     CASE OM_DISPOSE      ;  RETURN mDispose  (cl,obj,msg)
  217.     CASE OM_SET          ;  RETURN mSet      (cl,obj,msg)
  218.     CASE OM_GET          ;  RETURN mGet      (cl,obj,msg)
  219.     CASE MUIM_AskMinMax  ;  RETURN mAskMinMax(cl,obj,msg)
  220.     CASE MUIM_Setup      ;  RETURN mSetup    (cl,obj,msg)
  221.     CASE MUIM_Cleanup    ;  RETURN mCleanup  (cl,obj,msg)
  222.     CASE MUIM_Draw       ;  RETURN mDraw     (cl,obj,msg)
  223.   ENDSELECT
  224.  
  225. ENDPROC doSuperMethodA(cl,obj,msg)
  226.  
  227.  
  228.  
  229. /***************************************************************************/
  230. /* Thats all there is about it. Now lets see how things are used...        */
  231. /***************************************************************************/
  232.  
  233. PROC main() HANDLE
  234. DEF app=NIL,window,myobj,pen,
  235.     mcc=NIL:PTR TO mui_customclass,
  236.     startpen:PTR TO mui_penspec,
  237.     sigs=0
  238.  
  239.   IF (muimasterbase:=OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN))=NIL THEN
  240.      Raise('Failed TO open muimaster.library')
  241.  
  242.   /*
  243.   ** open utility.library, because we need function NextTagItem()
  244.   */
  245.   IF (utilitybase:=OpenLibrary('utility.library',36))=NIL THEN
  246.      Raise('Failed TO open utility.library')
  247.  
  248.  
  249.   /* Create the NEW custom class with a call TO MUI_CreateCustomClass(). */
  250.   /* Caution: This function returns not a struct IClass, BUT a           */
  251.   /* struct MUI_CustomClass which contains a struct IClass TO be         */
  252.   /* used with NewObject() calls.                                        */
  253.   /* Note well: MUI creates the dispatcher hook FOR you, you may         */
  254.   /* *not* use its h_Data field! IF you need custom data, use the        */
  255.   /* cl_UserData OF the IClass structure!                                */
  256.  
  257.   /* E-note: Create the NEW custom class with a call TO eMui_CreateCustomClass().*/
  258.   /* Big thanks TO Jan Hendrik Schulz FOR creating eMui_CreateCustomClass()      */
  259.  
  260.   IF (mcc:=eMui_CreateCustomClass(NIL,MUIC_Area,NIL,SIZEOF mydata,{myDispatcher}))=NIL THEN
  261.       Raise('Could not create custom class.')
  262.  
  263.   app:=ApplicationObject,
  264.       MUIA_Application_Title      , 'Class2',
  265.       MUIA_Application_Version    , '$VER: Class2 12.9 (21.11.95)',
  266.       MUIA_Application_Copyright  , '©1995, Stefan Stuntz',
  267.       MUIA_Application_Author     , 'Stefan Stuntz',
  268.       MUIA_Application_Description, 'Demonstrate the use OF custom classes.',
  269.       MUIA_Application_Base       , 'CLASS2',
  270.  
  271.       SubWindow, window:=WindowObject,
  272.           MUIA_Window_Title, 'Another Custom Class',
  273.           MUIA_Window_ID   , "CLS2",
  274.           WindowContents, VGroup,
  275.  
  276.               Child, TextObject,
  277.                   TextFrame,
  278.                   MUIA_Background, MUII_TextBack,
  279.                   -> E-note : center this text means inserting a <ESC c> which IS usually \ec
  280.                   MUIA_Text_Contents, '\ecThis IS a custom class with attributes.\nClick on the button at the bottom of\nthe window TO adjust the color.',
  281.                  End,
  282.  
  283.               Child, myobj:=NewObjectA(mcc.mcc_class,NIL,
  284.                                 [TextFrame,
  285.                                  MUIA_Background, MUII_BACKGROUND,
  286.                                  TAG_DONE,0]),
  287.  
  288.               Child, HGroup, MUIA_Weight, 10,
  289.                   Child, FreeLabel('Custom Class Color:'),
  290.                   Child, pen:=PoppenObject,
  291.                       MUIA_CycleChain, 1,
  292.                       MUIA_Window_Title, 'Custom Class Color',
  293.                      End,
  294.               End,
  295.  
  296.           End,
  297.  
  298.       End,
  299.   End
  300.  
  301.   IF app=NIL THEN Raise('Failed to create Application.')
  302.  
  303.   doMethodA(window,[MUIM_Notify,MUIA_Window_CloseRequest,MUI_TRUE,
  304.             app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit])
  305.  
  306.   doMethodA(pen,[MUIM_Notify,MUIA_Pendisplay_Spec,MUIV_EveryTime,
  307.             myobj,3,MUIM_Set,MYATTR_PEN,MUIV_TriggerValue])
  308.  
  309.   get(pen,MUIA_Pendisplay_Spec,{startpen})
  310.   set(myobj,MYATTR_PEN,startpen)
  311.  
  312. /*
  313. ** This is the ideal input loop for an object oriented MUI application.
  314. ** Everything is encapsulated in classes, no return ids need to be used,
  315. ** we just check if the program shall terminate.
  316. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  317. ** from Wait() (or 0). This makes the input loop significantly faster.
  318. */
  319.  
  320.     set(window,MUIA_Window_Open,MUI_TRUE)
  321.  
  322.     WHILE Not(doMethodA(app,[MUIM_Application_NewInput,{sigs}]) = MUIV_Application_ReturnID_Quit)
  323.       IF sigs THEN sigs := Wait(sigs)
  324.     ENDWHILE
  325.  
  326.     set(window,MUIA_Window_Open,FALSE)
  327.  
  328. /*
  329. ** Shut down...
  330. */
  331.  
  332. EXCEPT DO
  333.     IF app THEN Mui_DisposeObject(app)                /* dispose all objects. */
  334.     IF mcc THEN Mui_DeleteCustomClass(mcc)            /* delete the custom class. */
  335.     IF utilitybase THEN CloseLibrary(utilitybase)
  336.     IF muimasterbase THEN CloseLibrary(muimasterbase) /* close library */
  337.     IF exception THEN WriteF('\s\n',exception)
  338. ENDPROC
  339.  
  340.